<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Feed_reports extends CI_Controller {

	public $data = array();

	function __construct()
	{
		parent::__construct();

		if( !$this->session->userdata('logged_in') )
			redirect('login');

		$this->load->model('merchant_model');
		$this->load->config('regions');
		$this->load->library('form_validation');
		$this->load->library('curl');
		$this->load->helper('form');
		$this->load->helper('download');

		$this->load->model('feeds_model');
	    $this->load->library("pagination");
	    $this->load->library("table");
	}

		
	function index( $mid = 0, $offset=0 )
	{
		$mid = (int)$mid;

		if( !$mid )
		{
			redirect('feed_reports/merchant');
		}

		//get merchant details
		$merchant_details = $this->merchant_model->get_by_id( $mid );

		if( !count($merchant_details) )
		{
			redirect('feed_reports/merchant');
		}

		$this->data['merchant_details'] = $merchant_details;

	    
		$total_count = $this->feeds_model->get_count($merchant_details['merchant_id'],$search_term="");

	    // start of pagination
	    $config['base_url'] = site_url('feed_reports/index/'.$mid);
	    $config['per_page'] = 25;
	    $config['total_rows'] = $total_count;
	    $config["uri_segment"] = 4;
	    $config['num_links'] = 5;
	    $config['prev_link'] = '&lt;Prev';
	    $config['next_link'] = 'Next&gt;';

	    $this->pagination->initialize($config);

	    $feed_data = $this->feeds_model->get_feed_data($merchant_details['merchant_id'],$config['per_page'],$offset,$search_term="");

	    $this->data['records'] = $feed_data;
		
		$this->layout->view('feeds/list', $this->data);
	}


	function merchant()
	{
		$this->data['error'] = '';
	
		//get Merchants List
		$result = $this->merchant_model->get_all();
	
		$this->data['merchants'] = array();
		$this->data['merchants'][''] = 'select';
		foreach ($result['rows'] as $row)
		{
			$this->data['merchants'][$row['id']] = $row['name'];
		}
	
	
		$this->form_validation->set_rules('merchant_id', 'Merchant', 'required');
	
		if($this->form_validation->run())
		{
			$merchant_id = $this->input->post('merchant_id');
	
			redirect('feed_reports/index/'.$merchant_id);
		}
	
		$this->layout->view('reports/view', $this->data);
	}
	

	function get_report( $feed_id = '' , $mid = '' )
	{
		$mid = (int)$mid;
		
		if( !$mid )
		{
			redirect('feed_reports/merchant');
		}
		
		//get merchant details
		$merchant_details = $this->merchant_model->get_by_id( $mid );
		
		if( !count($merchant_details) )
		{
			redirect('feed_reports/merchant');
		}
		
		$this->data['merchant_details'] = $merchant_details;
		
		$report_url = site_url("api/feeds/getFeedSubmissionResult/format/json?FeedSubmissionId={$feed_id}&KEY={$merchant_details['merchant_id']}");
		//die($report_url);
		$result = $this->get_api_data( $report_url );
		
		$result = json_decode($result, TRUE);
		
		if( isset($result['filePath']) )
		{
			$tmp = explode('/',$result['filePath']);
			force_download(end($tmp), file_get_contents($result['filePath']));
		}
		else
		{
			force_download('eeror.txt', 'There was an Error in generating report.');
		}
		
		
		
	}
	
	function get_api_data( $url = '')
	{
		$this->curl->create($url);
        $this->curl->option('SSL_VERIFYPEER', false); // for ssl
        $this->curl->option('RETURNTRANSFER', true);
        $data = $this->curl->execute();
		
        return $data;
        
	}

	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
